extern crate crypto;

use crypto::digest::Digest;
use crypto::sha2::Sha256;

use validator::ValidationErrors;
use validator::ValidationErrorsKind::Field;

// 获取所有的验证错误
pub fn get_all_fields(e: &ValidationErrors) -> Vec<String> {
    let mut err_arr = vec![];
    for (field, errors) in e.errors() {
        match errors {
            Field(errors) => {
                for error in errors {
                    let err_str = format!("{} err {}", field, error.code);
                    err_arr.push(err_str);
                }
            }
            _ => println!("Non-field error on field: {}", field),
        }
    }
    err_arr
}

// 加密
pub fn encrypt(message: &str) -> String {
    // 盐
    let salt = "4feb1c57488a058d08b2859e7596bc57f64d4dbc38156f0c807bd8696d08fc96";
    // 创建一个 SHA-256 对象
    let mut hasher = Sha256::new();
    // 添加盐和输入数据
    hasher.input_str(&(salt.to_owned() + &message));
    // 读取哈希结果
    let output = hasher.result_str();
    output
}
